home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C++ / Libraries / CModalProgress / CTestDialogs.cp < prev    next >
Encoding:
Text File  |  1994-09-01  |  7.9 KB  |  353 lines  |  [TEXT/MMCC]

  1. // ===========================================================================
  2. //    CTestDialogs.cp
  3. //
  4. //    This file contains the definition information for the example code to
  5. //    demonstrate the functionality of the CModalProgress class.
  6. //
  7. //    Copyright 1994 Alysoft Solutions. All rights reserved.
  8. //
  9. // ===========================================================================
  10.  
  11. #include "CTestDialogs.h"
  12.  
  13. /*
  14.  *  TEST 1
  15.  *    This test dialog simply shows a piece of text, a standard one state progress bar and a 
  16.  *    stop, or abort button.
  17.  */
  18.  
  19. void TestDialog1()
  20. {
  21.     CModalProgress            *dlg ;
  22.     long                    total = 0 ;
  23.     short                    progress = kDialogContinues ;
  24.     long                    delay ;
  25.     short                    stateSpace ;
  26.     
  27.     dlg = new CModalProgress ;
  28.     if (dlg->SetupDialog(kTest1DialogResID) != noErr)
  29.     {
  30.         SysBeep(1) ;
  31.         return ;
  32.     }
  33.     dlg->SetProgressBar(kTest1ProgressBarItem) ;
  34.  
  35.     dlg->SetCurrentState(100) ;
  36.     dlg->SetStateSpace(300) ;
  37.     dlg->BeginModal() ;
  38.  
  39.     while (progress == kDialogContinues)
  40.     {
  41.         progress = dlg->ProcessModal() ;
  42.         Delay(1, &delay) ;
  43.         stateSpace = dlg->SetCurrentStateValue(total++) ;
  44.     }
  45.     
  46.     dlg->EndModal() ;
  47.     delete dlg ;
  48.  
  49. }
  50.  
  51. /*
  52.  *    TEST 2
  53.  *    This test shows a more complex scenario, there is some changing text in the dialog which
  54.  *    is modified at state change time, there is a progress bar and there are multiple state
  55.  *    spaces.
  56.  */
  57.     
  58. void CDialog2::SetCurrentState(float statePercent)
  59. {
  60.     short                    itemType ;
  61.     Handle                    theItem ;
  62.     Rect                    box ;
  63.     
  64.     GetDialogItem(fDialog, kTest2ItemText, &itemType, &theItem, &box) ;
  65.     
  66.     CModalProgress::SetCurrentState(statePercent) ;
  67.  
  68.     if (statePercent == kTest2State1)
  69.     {
  70.         SetIText(theItem, kTest2State1String) ;
  71.         SetStateSpace(kTest2State1StateSpace) ;
  72.     }
  73.         
  74.     if (statePercent == kTest2State2)
  75.     {
  76.         SetIText(theItem, kTest2State2String) ;
  77.         SetStateSpace(kTest2State2StateSpace) ;
  78.     }
  79.     
  80.     if (statePercent == kTest2State3)
  81.     {
  82.         SetIText(theItem, kTest2State3String) ;
  83.         SetStateSpace(kTest2State3StateSpace) ;
  84.     }
  85. }
  86.  
  87. void TestDialog2()
  88. {
  89.     CDialog2                *dlg ;
  90.     long                    total = 0 ;
  91.     short                    progress = kDialogContinues ;
  92.     long                    delay ;
  93.     short                    stateSpace = kStateSpaceWithinLimit ;
  94.     
  95.     dlg = new CDialog2 ;
  96.     if (dlg->SetupDialog(kTest2DialogResID) != noErr)
  97.     {
  98.         SysBeep(1) ;
  99.         return ;
  100.     }
  101.     dlg->SetProgressBar(kTest2ProgressBarItem) ;
  102.  
  103.     dlg->SetCurrentState(kTest2State1) ;
  104.     dlg->BeginModal() ;
  105.  
  106.     while ((progress == kDialogContinues) && (stateSpace == kStateSpaceWithinLimit))
  107.     {
  108.         progress = dlg->ProcessModal() ;
  109.         Delay(1, &delay) ;
  110.         stateSpace = dlg->SetCurrentStateValue(total++) ;
  111.     }
  112.     
  113.     if (progress == kDialogContinues)
  114.     {
  115.         dlg->SetCurrentState(kTest2State2) ;
  116.         total = 0 ;
  117.         stateSpace = kStateSpaceWithinLimit ;
  118.         while ((progress == kDialogContinues) && (stateSpace == kStateSpaceWithinLimit))
  119.         {
  120.             progress = dlg->ProcessModal() ;
  121.             Delay(1, &delay) ;
  122.             stateSpace = dlg->SetCurrentStateValue(total++) ;
  123.         }
  124.         
  125.         if (progress == kDialogContinues)
  126.         {
  127.             dlg->SetCurrentState(kTest2State3) ;
  128.             total = 0 ;
  129.             while (progress == kDialogContinues)
  130.             {
  131.                 progress = dlg->ProcessModal() ;
  132.                 Delay(1, &delay) ;
  133.                 stateSpace = dlg->SetCurrentStateValue(total++) ;
  134.             }
  135.         }
  136.     }
  137.     
  138.     dlg->EndModal() ;
  139.     delete dlg ;
  140.  
  141. }
  142.  
  143. /*
  144.  *  TEST 3
  145.  *    A simple search type dialog with the infinite progress bar.
  146.  */
  147.  
  148. void TestDialog3()
  149. {
  150.     CModalProgress            *dlg ;
  151.     short                    progress = kDialogContinues ;
  152.     
  153.     dlg = new CModalProgress ;
  154.     if (dlg->SetupDialog(kTest3DialogResID) != noErr)
  155.     {
  156.         SysBeep(1) ;
  157.         return ;
  158.     }
  159.     dlg->SetInfiniteBar(kTest3InfiniteBarItem) ;
  160.  
  161.     dlg->BeginModal() ;
  162.  
  163.     while (progress == kDialogContinues)
  164.     {
  165.         progress = dlg->ProcessModal() ;
  166.     }
  167.     
  168.     dlg->EndModal() ;
  169.     delete dlg ;
  170.  
  171. }
  172.  
  173. /*
  174.  *    TEST 4
  175.  *    This is the most detailed of all the test. It comprises of a dialog which depicts a 
  176.  *    situation where the user is connecting to a remote server, downloading several files,
  177.  *    setting some parameter data on the server and then disconnecting from the server.
  178.  *    The dialog shows fields for the textual current state, the files being downloaded and
  179.  *    a progress bar to show time left.
  180.  */
  181.  
  182. void CDialog4::SetCurrentState(float statePercent)
  183. {
  184.     short                    itemType ;
  185.     Handle                    theItem ;
  186.     Rect                    box ;
  187.     
  188.     GetDialogItem(fDialog, kTest4StateTextItem, &itemType, &theItem, &box) ;
  189.     
  190.     CModalProgress::SetCurrentState(statePercent) ;
  191.  
  192.     switch ((long)statePercent)
  193.     {
  194.         case kTest4ConnectState:
  195.             SetIText(theItem, kTest4ConnectStateString) ;
  196.             SetStateSpace(kTest4StandardStateSpace) ;
  197.             break ;
  198.             
  199.         case kTest4LoginState:
  200.             SetIText(theItem, kTest4LoginStateString) ;
  201.             SetStateSpace(kTest4StandardStateSpace) ;
  202.             break ;
  203.             
  204.         case kTest4DownloadState:
  205.             SetIText(theItem, kTest4DownloadStateString) ;
  206.             SetStateSpace(kTest4DownloadStateSpace) ;
  207.             break ;
  208.             
  209.         case kTest4SetParamsState:
  210.             SetIText(theItem, kTest4SetParamsStateString) ;
  211.             SetStateSpace(kTest4StandardStateSpace) ;
  212.             break ;
  213.             
  214.         case kTest4LogoffState:
  215.             SetIText(theItem, kTest4LogoffStateString) ;
  216.             SetStateSpace(kTest4StandardStateSpace) ;
  217.             break ;
  218.             
  219.         case kTest4DisconnectState:
  220.             SetIText(theItem, kTest4DisconnectStateString) ;
  221.             SetStateSpace(kTest4StandardStateSpace) ;
  222.             break ;
  223.             
  224.         case kTest4CleanupState:
  225.             SetIText(theItem, kTest4CleanupStateString) ;
  226.             SetStateSpace(kTest4StandardStateSpace) ;
  227.             break ;
  228.             
  229.     }
  230. }
  231.  
  232. void CDialog4::SetFileName(Str255 filename)
  233. {
  234.     short                    itemType ;
  235.     Handle                    theItem ;
  236.     Rect                    box ;
  237.     
  238.     GetDialogItem(fDialog, kTest4FileNameTextItem, &itemType, &theItem, &box) ;
  239.     SetIText(theItem, filename) ;
  240. }
  241.  
  242. short PerformAState(CDialog4 *dlg, short whichState)
  243. {
  244.     short                    progress = kDialogContinues ;
  245.     short                    stateSpace = kStateSpaceWithinLimit ;
  246.     long                    total = 0 ;
  247.     
  248.     dlg->SetCurrentState(whichState) ;
  249.  
  250.     while ((stateSpace == kStateSpaceWithinLimit) && (progress == kDialogContinues))
  251.     {
  252.         progress = dlg->ProcessModal() ;
  253.         stateSpace = dlg->SetCurrentStateValue(total++) ;
  254.     }
  255.  
  256.     return progress ;
  257. }
  258.  
  259. void TestDialog4()
  260. {
  261.     CDialog4                *dlg ;
  262.     short                    progress = kDialogContinues ;
  263.     long                    total ;
  264.     
  265.     dlg = new CDialog4 ;
  266.     if (dlg->SetupDialog(kTest4DialogResID) != noErr)
  267.     {
  268.         SysBeep(1) ;
  269.         return ;
  270.     }
  271.     
  272.     dlg->SetProgressBar(kTest4ProgressBarItem) ;
  273.     dlg->SetPercentText(kTest4PercentTextItem, 5) ;
  274.     
  275.     dlg->SetFileName(kTest4DownloadFileNone) ;
  276.     
  277.     dlg->BeginModal() ;
  278.     
  279.     /* Process the connect state */
  280.     
  281.     if (PerformAState(dlg, kTest4ConnectState) != kDialogContinues)
  282.         goto ALL_DONE ;
  283.     
  284.     /* Process the login state */
  285.     
  286.     if (PerformAState(dlg, kTest4LoginState) != kDialogContinues)
  287.         goto ALL_DONE ;
  288.     
  289.     /* Perform the file downloads */
  290.     
  291.     dlg->SetCurrentState(kTest4DownloadState) ;
  292.     dlg->SetFileName(kTest4DownloadFile1) ;
  293.     
  294.     total = 0 ;
  295.     while ((progress == kDialogContinues) && (total < kTest4File1StateSpace))
  296.     {
  297.         progress = dlg->ProcessModal() ;
  298.         dlg->SetCurrentStateValue(total++) ;
  299.     }
  300.     if (progress != kDialogContinues)
  301.         goto ALL_DONE ;
  302.     
  303.     /* File 2 */
  304.     
  305.     dlg->SetFileName(kTest4DownloadFile2) ;
  306.     
  307.     while ((progress == kDialogContinues) && (total < (kTest4File2StateSpace + kTest4File1StateSpace)))
  308.     {
  309.         progress = dlg->ProcessModal() ;
  310.         dlg->SetCurrentStateValue(total++) ;
  311.     }
  312.     if (progress != kDialogContinues)
  313.         goto ALL_DONE ;
  314.  
  315.     /* File 3 */
  316.     
  317.     dlg->SetFileName(kTest4DownloadFile3) ;
  318.     
  319.     while ((progress == kDialogContinues) && (total < (kTest4File2StateSpace + kTest4File1StateSpace + kTest4File3StateSpace)))
  320.     {
  321.         progress = dlg->ProcessModal() ;
  322.         dlg->SetCurrentStateValue(total++) ;
  323.     }
  324.     if (progress != kDialogContinues)
  325.         goto ALL_DONE ;
  326.  
  327.     dlg->SetFileName(kTest4DownloadFileNone) ;
  328.  
  329.     /* Perfrom the Parameter setting */
  330.  
  331.     if (PerformAState(dlg, kTest4SetParamsState) != kDialogContinues)
  332.         goto ALL_DONE ;
  333.     
  334.     /* Perfrom the Logoff state */
  335.  
  336.     if (PerformAState(dlg, kTest4LogoffState) != kDialogContinues)
  337.         goto ALL_DONE ;
  338.  
  339.     /* Perfrom the disconnect state */
  340.  
  341.     if (PerformAState(dlg, kTest4DisconnectState) != kDialogContinues)
  342.         goto ALL_DONE ;
  343.     
  344.     /* Perfrom the cleanup state */
  345.  
  346.     if (PerformAState(dlg, kTest4CleanupState) != kDialogContinues)
  347.         goto ALL_DONE ;
  348.  
  349. ALL_DONE:
  350.     dlg->EndModal() ;
  351.     delete dlg ;
  352. }
  353.